<template>
  <van-form @submit="onSubmit">
    <van-field
        v-model="editUser.currentValue"
        :name="editUser.editKey"
        :label="editUser.editName"
        placeholder="`请输入${editUser.editName}`"
    />
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>


<script setup lang="ts">
import {onMounted, ref} from "vue";
import {useRoute, useRouter} from "vue-router";
import request from '../../utils/request'
import {showToast} from 'vant';
import {currentUser, updateUser} from "../../services/api.ts";


const route = useRoute();
const router = useRouter();
const editUser = ref({
  editKey: route.query.editKey,
  currentValue: route.query.currentValue,
  editName: route.query.editName,
})
const user = ref({})

onMounted(async () => {
  const res = await currentUser()
  if (res.code === 0) {
    user.value = res.data
  } else {
    showToast(res.description)
  }

})


const onSubmit = async () => {


  const res = await updateUser({
    id: user.value.id,
    [editUser.value.editKey]: editUser.value.currentValue
  })
  // console.log(res)
  if (res.code === 0) {
    showToast(res.description);
    router.back()
  } else {
    showToast(res.description);
  }

};
</script>


<style scoped>

</style>